package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.admin.model.JobModel;
import com.zhongyouyun.admin.pojo.JobPojo;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.*;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.JobService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2022-12-16
 * @Description: job配置执行类
 */
@RestController
@RequestMapping("/admin/z_job")
//@Api(value = "定时任务管理", tags = {"定时任务管理"})
public class JobController {
    @Autowired
    private JobService service;

    /**
     * 定时任务列表
     */
    @PostMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "获取定时任务信息", notes = "取定时任务信")
    public IPage<JobPojo> findPage(@RequestBody PageRequest pageRequest) {
        QueryWrapper<JobPojo> queryWrapper = new QueryWrapper();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        Page page = new Page(pageRequest.getPageNum(), pageRequest.getPageSize());
        return service.selectPojoPage(page, queryWrapper);
    }

    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody JobModel jobModel, HttpServletRequest request) {
        String type = request.getHeader("xiangmuType");
        jobModel.setXiangMuType(type);
        ///UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(jobModel.getId())) {
            jobModel.setCreateTime(new Date());
        } else {
            jobModel.setUpdateTime(new Date());
        }
        if (CronExpression.isValidExpression(jobModel.getCron())) {
            //新增修改定时器
            Boolean flag = service.saveforUpdate(jobModel);
            if (flag) {
                return Result.builder().message("成功").status(0).build();
            }
        } else {
            return Result.builder().message("cron表达式错误").success(false).status(1).build();
        }


        return Result.builder().message("失败").status(1).success(false).build();
    }

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result delete(@ApiParam(name = "定时任务ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        Boolean flag = false;
        flag = service.removeById(Long.valueOf(id));
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "批量删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<String> ids) {

        List<Long> list = new ArrayList<Long>();
        ids.forEach(id -> {
            list.add(Long.valueOf(id));
        });
        Boolean flag = service.removeforByIds(list);
        if (flag) {
            flag = service.removeByIds(list);
            if (flag) {
                return Result.builder().message("成功").status(0).build();
            } else {
                return Result.builder().message("删除失败").success(false).status(1).build();
            }
        }
        return Result.builder().message("删除失败").success(false).status(0).build();
    }
}
